import { ETaskState } from "GameLoop/DecisionMaking/TaskDeclare/type";
import { ATaskAdapter } from ".";
import { ReactionState } from "GameLoop/WorldState/EntityManager/Entitys/room/lab/types";
import { LabTaskHelper } from "utils/task/LabTaskHelper";
import { CoreEntity } from "GameLoop/WorldState/EntityManager/Entitys";

export class ReactionTaskAdapter extends ATaskAdapter<"react">
{
    // constructor(context:IContextGetter,world:WorldGetter)
    // {
    //     super(context,world);
       
    // }
 
    public bindTasks(room: Room, tasks:ITaskDeclare["react"][],customCheck:IConditionFunction): void 
    {
        for(const task of tasks)
        {
            if(customCheck(room as Required<Room>,task) == OK )
            {
                room.deleteTaskById(task.id,"react");
               
            }
            else if(ETaskState.Complete == this.checkState(room,task))
            {
                //global.Log.Info(`${room.name} room ${task.id} check 已经完成`);
                room.deleteTaskById(task.id,"react");
           
            }
            // global.Log.Info(`${room.name} room ${task.id} check 未完成`);
            break;
        }
    }

    protected checkState(room:Room,task:ITaskDeclare["react"]):ETaskState
    {
        const labEntity = room.getEntity("LabEntity");
        if(!labEntity.getNodeMemory().reactionTask)
        {
            labEntity.getNodeMemory().reactionTask =task.id
        }
        if(labEntity.getNodeMemory().reactionTask == task.id)
        {
            // 是当前任务可以合成
            if(task.data.arg.State == ReactionState.init)
            {
                // 还没有初始化
                const coreEntity = room.getEntity("CoreEntity");
                const store = this.getStore(coreEntity);
                task.data.arg.Reactions = LabTaskHelper.CreateReationStack({target:task.data.arg.RectionTarget,number:task.data.arg.TargetCount},store);
                task.data.arg.State = ReactionState.Idle;

            }
            else
            {
                // 1. 超时判断 完成
                {
                    
                }

                // 2.更新反应栈
                const react = LabTaskHelper.GetReactant(task.data.arg);
                if(react)
                {
                    if(react.Reactant.amount !==undefined  &&react.Reactant.amount>= react.Reactant.number )
                    {
                        if(react.Reactant.amount <0)
                        {
                            return ETaskState.Complete;
                        }
                        task.data.arg.Reactions.splice( task.data.arg.Reactions.length-1,1);
                       // global.LogDebug(`反应结束删除任务! ${JSON.stringify(toptask)}`);
                    }
                }
                if(task.data.arg.Reactions.length == 0  )
                {
                    // 反应完成，删除任务
                    return ETaskState.Complete;
                }
            }
            return ETaskState.Running;
        }
        else
        {
            
            return ETaskState.Running;
        }
    }
    private getStore(entity:CoreEntity)
    {
        if(entity.Storage && entity.Terminal)
        {
            const store = _.clone(entity.Storage.store);
            for(let  key in entity.Terminal.store)
            {
                if(store[key as ResourceConstant])
                {
                    store[key as ResourceConstant]+=entity.Terminal.store[key as ResourceConstant]
                }
                else
                {
                    store[key as ResourceConstant]=entity.Terminal.store[key as ResourceConstant]
                }
            }
            return store
        }
        else if (entity.Storage)
        {
            return entity.Storage.store;   
        }else if (entity.Terminal)
        {
            return entity.Terminal.store;   
        }
        return undefined
    }
}